package com.salesys.gr.gui;

import java.util.ArrayList;
import java.util.List;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import com.salesys.gr.util.FrmHelper;
import com.salesys.tony.domain.Record;
import com.salesys.tony.service.RecordService;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.TraverseEvent;

public class FrmViewMarkRecord {

	protected Shell FrmViewMarkRecord;
	private Table tableMarkRecord;
	private int queryId;

	public void transfer(int loginedUserId) {
		queryId = loginedUserId;
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		FrmViewMarkRecord.open();
		FrmViewMarkRecord.layout();
		while (!FrmViewMarkRecord.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * 
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		FrmViewMarkRecord = new Shell(SWT.CLOSE | SWT.APPLICATION_MODAL);
		FrmViewMarkRecord.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 27) {
					FrmViewMarkRecord.close();
				}
			}
		});
		FrmViewMarkRecord.setSize(400, 300);
		FrmViewMarkRecord.setText("查看现金纪录");
		// 窗口居中
		FrmViewMarkRecord.setLocation(new FrmHelper().setLocationCenter(FrmViewMarkRecord));

		tableMarkRecord = new Table(FrmViewMarkRecord, SWT.BORDER | SWT.FULL_SELECTION);
		tableMarkRecord.setBounds(0, 0, 394, 272);
		tableMarkRecord.setHeaderVisible(true);
		tableMarkRecord.setLinesVisible(true);

		TableColumn tblclmnMarkSource = new TableColumn(tableMarkRecord, SWT.NONE);
		tblclmnMarkSource.setWidth(80);
		tblclmnMarkSource.setText("现金来源");

		TableColumn tblclmnMark = new TableColumn(tableMarkRecord, SWT.NONE);
		tblclmnMark.setToolTipText("");
		tblclmnMark.setWidth(80);
		tblclmnMark.setText("增加金额");

		TableColumn tableColumn = new TableColumn(tableMarkRecord, SWT.NONE);
		tableColumn.setWidth(210);
		tableColumn.setText("时间");

		List<Record> recordList = new ArrayList<Record>();
		try {
			recordList = new RecordService().queryMarkRecordFromUserid(queryId + "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<TableItem> tbtmChild = new ArrayList<TableItem>();

		for (int i = 0; i < recordList.size(); i++) {
			tbtmChild.add(i, new TableItem(tableMarkRecord, SWT.NONE));
			if (recordList.get(i).getMarksource().equals("recommend")) {
				tbtmChild.get(i).setText(0, "推荐");
			} else if (recordList.get(i).getMarksource().equals("withdraw")) {
				tbtmChild.get(i).setText(0, "提现");
			} else {
				try {
					tbtmChild.get(i).setText(0, new RecordService().queryChildLevelFromMarkSource(recordList.get(i).getMarksource()) + "级加盟");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			tbtmChild.get(i).setText(1, recordList.get(i).getMark());
			tbtmChild.get(i).setText(2, recordList.get(i).getTime());
		}
	}
}
